package agenda;

public abstract class SlottedEntry extends Entry {

	/*** Constructors */
	protected SlottedEntry(Agenda<?> agenda, int day, String subject) {
		super(agenda, day, subject);
	}

	
	/*** Slots */
	public static final int LOWEST_SLOT = 1;
	public static final int HIGHEST_SLOT = 24;
	
	protected abstract boolean occupiesSlot(int slot);

	public static boolean isValidSlot(int slot) {
		return (slot >= LOWEST_SLOT) && (slot <= HIGHEST_SLOT);
	}

	public int getFirstSlot() {
		int slot = LOWEST_SLOT;
		while (! occupiesSlot(slot))
			slot++;
		return slot;
	}

	public int getLastSlot() {
		int slot = HIGHEST_SLOT;
		while (! occupiesSlot(slot))
			slot--;
		return slot;
	}
	
	/*** Timeable */
	@Override
	public long getStartTime() {
		return super.getStartTime() + (getFirstSlot()-1)*60*60*1000;
	}
	
	@Override
	public long getEndTime() {
		return super.getStartTime() + getLastSlot()*60*60*1000;
	}

	
	/*** General purpose */
	@Override
	public boolean overlapsWith(Entry other) {
		if (other == null)
			return false;
		if (this.getDay() != other.getDay())
			return false;
		if (other instanceof DayEvent)
			return other.overlapsWith(this);
		if ( (other instanceof Occupiable) && (! ((Occupiable) other).isOccupying()) )
			return false;
		SlottedEntry otherSlottedEntry = (SlottedEntry) other;
		int slot = getFirstSlot();
		while ( (slot <= HIGHEST_SLOT) && ( ! (this.occupiesSlot(slot) && otherSlottedEntry.occupiesSlot(slot))) )
				slot++;
		return slot <= HIGHEST_SLOT;
	}

	@Override
	public abstract SlottedEntry mergeWith(final Entry other);
}
